<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 10/14/14
 * Time: 10:13 PM
 */

namespace Backend\Modules\Video\Controllers;
use Common\library\Common;
use Library\Constant\Constant;
use \Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Models\VideoGroup;
use Backend\Modules\Video\Forms\FormSeachVideo;
use Backend\Modules\Video\Forms\FormSearchVideoGroup;

class AjaxHomeCategoryController extends \BackendController
{
    public function loadVideoAction($homeCategoryId)
    {
        $this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getQuery('page', 'int', 1));

        $videos = Video::find();

        $formSearchVideo = new FormSeachVideo();

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'data'  => $videos,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => '',
            )
        );
        $this->view->pager = $pager;
        $this->view->homeCategoryId = $homeCategoryId;
        $this->view->formSearchVideo = $formSearchVideo;
        $this->view->render("ajax-home-category", "modal/modal_video");
    }

    public function seachVideoAction()
    {
        if($this->request->isAjax())
        {
            $this->view->setTemplateAfter('empty');

            $currentPage = abs($this->request->getQuery('page', 'int', 1));

            //$videos = Video::find();
            $sqlWhere = '1 = 1';
            $conditionsWhere = array();

            if($this->request->hasPost('id') && $this->request->getPost('id'))
            {
                $sqlWhere .= ' AND video.id = :id:';
                $conditionsWhere['id'] = $this->request->getPost('id');
            }

            if($this->request->hasPost('name') && $this->request->getPost('name'))
            {
                $sqlWhere .= ' AND videoDescription.name LIKE :name:';
                $conditionsWhere['name'] = '%' . $this->request->getPost('name') . '%';
            }

            if($this->request->hasPost('category_id') && $this->request->getPost('category_id' , 'int') > 0)
            {
                $sqlWhere .= ' AND videoCategoryLink.category_id = :category_id:';
                $conditionsWhere['category_id'] = $this->request->getPost('category_id');
            }

            if($this->request->hasPost('national_id') && $this->request->getPost('national_id' , 'int') > 0)
            {
                $sqlWhere .= ' AND videoDescription.national_id = :national_id:';
                $conditionsWhere['national_id'] = $this->request->getPost('national_id' , 'int');
            }

            $videos = $this->modelsManager->createBuilder()
                ->from(array('video' => '\Backend\Modules\Video\Models\Video'))
                ->groupBy("video.id")
                ->join('\Backend\Modules\Video\Models\VideoDescription' , 'videoDescription.video_id = video.id' , 'videoDescription')
                ->join('\Backend\Modules\Video\Models\VideoCategoryLink' , 'videoCategoryLink.video_id = video.id' , 'videoCategoryLink')
                ->limit(Constant::ITEM_PER_PAGE_20)
                ->where($sqlWhere , $conditionsWhere)
                ->orderBy('video.id')
                ->getQuery()
                ->execute();

            if ($currentPage == 0) {
                $currentPage = 1;
            }

            $pager = new Pager(
                new Paginator(array(
                    'data'  => $videos,
                    'limit' => Constant::ITEM_PER_PAGE_20,
                    'page'  => $currentPage,
                )),
                array(
                    'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                    'rangeLength' => 5,
                    'urlMask'     => '',
                )
            );
            $this->view->pager = $pager;
            $this->view->render("ajax-video-group", "result_search_video");

            $json['success'] = true;
            $json['html'] = $this->view->getContent();
            $this->response->setContent(json_encode($json));
            //Return the response
            return $this->response->send();
        }
    }

    public function loadVideoGroupAction()
    {
       $this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $videoGroups = VideoGroup::find();

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'data'  => $videoGroups,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => '?page={%page_number}',
            )
        );
        $this->view->pager = $pager;
        $this->view->objectType = $this->request->get('object_type');
        $this->view->objectId = $this->request->get('object_id');
        $this->view->formSearchVideoGroup = new FormSearchVideoGroup();
        $this->view->render("ajax-video-group", "modal/modal_video_group");
    }
} 